{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pyecharts import options as opts\n",
    "import pyecharts\n",
    "from pyecharts.charts import Map, Timeline\n",
    "import pandas  as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'C:\\\\Users\\\\86178\\\\Desktop\\\\房地产建筑面积交互地图.html'"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = pd.read_csv(\"房地产施工面积.csv\", engine=\"python\")\n",
    "#导入数据\n",
    "\n",
    "data = data.set_index(\"地区\")\n",
    "\n",
    "#对数据进行存储\n",
    "p = []\n",
    "for i in data.index:\n",
    "    if i =='黑龙江省' or i =='内蒙古自治区':\n",
    "        p.append(i[:3])\n",
    "    elif i in [\"广西壮族自治区\",\"西藏自治区\",\"宁夏回族自治区\",\"新疆维吾尔自治区 \"]:\n",
    "        p.append(i[:2])\n",
    "    else:\n",
    "        p.append(i[:2])\n",
    "\n",
    "#封装交互函数\n",
    "def fatch_data(y):\n",
    "    v = [float(i) for i in data[y]]\n",
    "    # 生成可视化图表\n",
    "    c = (\n",
    "        Map(init_opts=opts.InitOpts(width='1200px', height='600px')) # 设置地图大小\n",
    "            .add(\"\", [tuple(z) for z in zip(p, v)], \"china\", # 输入pyecharts.map 需要的地图数据\n",
    "                 label_opts=opts.LabelOpts(formatter=\"{b}\\n {c}\")) \n",
    "            .set_global_opts(\n",
    "            title_opts=opts.TitleOpts(title=\"房地产面积交互可视化\"), # 输入可视化图表标题\n",
    "            visualmap_opts=opts.VisualMapOpts( is_piecewise=True,max_=60000, min_=300, pieces=5),\n",
    "\n",
    "        )\n",
    "    )\n",
    "    return c\n",
    "\n",
    "\n",
    "timeline = Timeline(init_opts=opts.InitOpts(width=\"1600px\", height=\"800px\"))\n",
    "#添加timeline拖动条,增加交互效果\n",
    "\n",
    "for y in data.columns[::-1]:\n",
    "    timeline.add(fatch_data(y), time_point=str(y))\n",
    "timeline.add_schema(is_auto_play=False, play_interval=1000)# 设置可视化图标的自动\n",
    "\n",
    "#导出可视化图\n",
    "timeline.render(\"房地产建筑面积交互地图.html\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'C:\\\\Users\\\\86178\\\\Desktop\\\\房地产投资商交互地图.html'"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = pd.read_csv(\"房地产投资商.csv\", engine=\"python\")\n",
    "data = data.set_index(\"地区\")\n",
    "#对数据进行存储\n",
    "p = []\n",
    "for i in data.index:\n",
    "    if i =='黑龙江省' or i =='内蒙古自治区':\n",
    "        p.append(i[:3])\n",
    "    elif i in [\"广西壮族自治区\",\"西藏自治区\",\"宁夏回族自治区\",\"新疆维吾尔自治区 \"]:\n",
    "        p.append(i[:2])\n",
    "    else:\n",
    "        p.append(i[:2])\n",
    "\n",
    "#封装交互函数\n",
    "def fatch_data(y):\n",
    "    v = [float(i) for i in data[y]]\n",
    "    # 生成可视化图表\n",
    "    c = (\n",
    "        Map(init_opts=opts.InitOpts(width='1200px', height='600px')) # 设置地图大小\n",
    "            .add(\"\", [tuple(z) for z in zip(p, v)], \"china\", # 输入pyecharts.map 需要的地图数据\n",
    "                 label_opts=opts.LabelOpts(formatter=\"{b}\\n {c}\")) \n",
    "            .set_global_opts(\n",
    "            title_opts=opts.TitleOpts(title=\"房地产投资交互分析\"), # 输入可视化图表标题\n",
    "            visualmap_opts=opts.VisualMapOpts( is_piecewise=True,max_=16000, min_=1, pieces=5),\n",
    "\n",
    "        )\n",
    "    )\n",
    "    return c\n",
    "\n",
    "\n",
    "timeline = Timeline(init_opts=opts.InitOpts(width=\"1600px\", height=\"800px\"))\n",
    "#添加timeline拖动条,增加交互效果\n",
    "\n",
    "for y in data.columns[::-1]:\n",
    "    timeline.add(fatch_data(y), time_point=str(y))\n",
    "timeline.add_schema(is_auto_play=False, play_interval=1000)# 设置可视化图标的自动\n",
    "\n",
    "#导出可视化图\n",
    "timeline.render(\"房地产投资商交互地图.html\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
